package od.igor.petrov.utils;

/* ================================== *
 *  SORTING BY ITEM NAME AND ORDER
 * ================================== */

import java.util.Comparator;

public class TestTableBeanItemComparator implements Comparator<TestTableBean> {

	public TestTableBeanItemComparator(boolean orderDesc){
		this.orderDesc = orderDesc;
	}
	
	public int compare(TestTableBean bean1, TestTableBean bean2) {
		String firstBeanItem = bean1.getItem();
		String secondBeanItem = bean2.getItem();
		int n1 = firstBeanItem.length();
		int n2 = secondBeanItem.length();
		int min = Math.min(n1, n2);
		for (int i = 0; i < min; i++) {
			char c1 = firstBeanItem.charAt(i);
			char c2 = secondBeanItem.charAt(i);
			if (c1 != c2) {
				c1 = Character.toUpperCase(c1);
				c2 = Character.toUpperCase(c2);
				if (c1 != c2) {
					c1 = Character.toLowerCase(c1);
					c2 = Character.toLowerCase(c2);
					if (c1 != c2 && !orderDesc) {
						return c1 - c2;
					} else if (c1 != c2 && orderDesc){
						return c2 - c1;
					}
				}
			}
		}
		if(!orderDesc){
			return n1 - n2;
		} else {
			return n2 - n1;
		}
	}
	
	private boolean orderDesc = false;
}